package com.terabits.smartriver.service.impl;

import cn.hutool.core.convert.Convert;
import com.sun.org.apache.regexp.internal.RE;
import com.terabits.smartriver.constants.InspectionEnum;
import com.terabits.smartriver.mapper.InspectionMapper;
import com.terabits.smartriver.mapper.InspectionPaperMapper;
import com.terabits.smartriver.mapper.InspectionTopicMapper;
import com.terabits.smartriver.meta.vo.PaperVO;
import com.terabits.smartriver.meta.dto.TopicDTO;
import com.terabits.smartriver.meta.po.InspectionPaper;
import com.terabits.smartriver.meta.po.InspectionTopic;
import com.terabits.smartriver.service.InspectioService;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.RequestHandler;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.String.*;

/**
 * Created by Yin on 2019/3/11
 */
@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class InspectioServiceImpl implements InspectioService {
    @Autowired
    private InspectionTopicMapper inspectionTopicMapper;

    @Autowired
    private InspectionPaperMapper inspectionPaperMapper;

    @Autowired
    private InspectionMapper inspectionResultMapper;

    @Override
    public List getTopic(PageData pageData) {
        List<TopicDTO> topics = inspectionTopicMapper.selectTopic(pageData);
        return topics;
    }

    /**
     * 过时 更换为getTopic
     *
     * @param type
     * @param topicIds
     * @return
     */
    public Map getTopicOut(Integer type, String topicIds) {

        Map topics = new HashMap();
        PageData pageData = new PageData();

        //情况1  根据题号选择=================================================================
        if (topicIds != null) {
            pageData.put("topicIds", topicIds);
            List<TopicDTO> oneTopic = inspectionTopicMapper.selectTopic(pageData);
            if (oneTopic != null && oneTopic.size() > 0) {
                //什么题型 放到什么里面 如果是选择题
                if (InspectionEnum.TOPIC_TYPE_CHOICE.getType().equals(oneTopic.get(0).getType())) {
                    topics.put("choice", oneTopic);
                } else {
                    topics.put("subjective", oneTopic);
                }
            }
            return topics;

        }
        //情况2   根据类型选择=================================================================
        if (type != null) {
            pageData.put("type", type);
            List<TopicDTO> choice = inspectionTopicMapper.selectTopic(pageData);
            if (InspectionEnum.TOPIC_TYPE_CHOICE.getType().equals(type)) {
                topics.put("choice", choice);
            } else {
                topics.put("subjective", choice);
            }
            return topics;
        }

        //情况3    全部返回====================================================================
        pageData.put("type", InspectionEnum.TOPIC_TYPE_CHOICE.getType());
        List<TopicDTO> choice = inspectionTopicMapper.selectTopic(pageData);
        pageData.put("type", InspectionEnum.TOPIC_TYPE_SUBJECTIVE.getType());
        List<TopicDTO> subjective = inspectionTopicMapper.selectTopic(pageData);
        topics.put("choice", choice);
        topics.put("subjective", subjective);
        return topics;
    }

    @Override
    public int addTopics(Integer userId, List<InspectionTopic> topics) {
        return inspectionTopicMapper.insertTopics(userId, topics);
    }

    @Override
    public List<PaperVO> getPaper(PageData pageData) {
        return inspectionPaperMapper.selectPaper(pageData);
    }

//    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        Thread thread = new Thread();
//        Thread thread1 = new Thread();
//        synchronized (thread) {
//            setData();
//        }
//        synchronized (thread1) {
//            setData();
//        }
//    }
//
//
//    public static void setData() {
//        Lock lock = new ReentrantLock();
//        String b = "2,3,1,6";
//        TreeSet<String> list = new TreeSet<>(Comparator.comparing(Convert::toStr));
//        for (String s : b.split(",")) {
//            list.add(s);
//
//        }
////        Collections.sort(list);
//        Iterator<String> stringListIterator = list.iterator();
//        while (stringListIterator.hasNext()) {
//            String next = stringListIterator.next();
//            if (next.equals("2")) {
//                stringListIterator.remove();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            lock.lock();
//                            Thread.sleep(2000);
//                            list.remove("1");
//                            System.out.println(list);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        } finally {
//                            lock.unlock();
//                        }
//                    }
//                }).start();
//            }
//        }
//        System.out.println(list);
//    }

    @Override
    public int addPaper(InspectionPaper inspectionPaper) {
        Integer integer = 0;
        String deviceNum = inspectionPaper.getDeviceNum();
        List<String> list = Arrays.asList(deviceNum);
        for (String num : list) {
            inspectionPaper.setDeviceNum(num);
            inspectionPaperMapper.insertSelective(inspectionPaper);
            integer++;
        }
        return integer;
    }

    @Override
    public int deleteTopics(Integer id) {
        return inspectionTopicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updatePaper(InspectionPaper inspectionPaper) {
        return inspectionPaperMapper.updateByPrimaryKeySelective(inspectionPaper);
    }

    @Override
    public int deletePaper(Integer id) {
        InspectionPaper inspectionPaper = new InspectionPaper();
        inspectionPaper.setId(id);
        int delete = inspectionPaperMapper.delete(inspectionPaper);
        return delete;
    }

    @Override
    public List<PageData> getResult(PageData pageData) {
        return inspectionResultMapper.getInspection(pageData);
    }

}
